package cz.proks.schoolpupilbook.gwt.widget.common;

import java.util.LinkedList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.schoolpupilbook.gwt.core.SchoolPupilBookFactory;

public class LoginForm extends Composite {

	private static LoginFormUiBinder uiBinder = GWT.create(LoginFormUiBinder.class);

	interface LoginFormUiBinder extends UiBinder<Widget, LoginForm> {
	}
	
	@UiField TextBox loginBox;
	@UiField PasswordTextBox passwordBox;
	@UiField Button submitButton;
	@UiField Errors errors;

	public LoginForm() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public String getLogin() {
		return loginBox.getValue();
	}
	
	public void setLogin(String login) {
		loginBox.setValue(login);
	}
	
	public String getPassword() {
		return passwordBox.getValue();
	}
	
	public void setPassword(String password) {
		passwordBox.setValue(password);		
	}
	
	@UiHandler("submitButton")
	public void submit(ClickEvent event) {
		LinkedList<String> errors = validate();
		if (errors.isEmpty()) {
			this.errors.setErrors(null);
			SubmitEvent submitEvent = new SubmitEvent(getLogin(), getPassword());
			SchoolPupilBookFactory.getEventBus().fireEvent(submitEvent);
		} else {
			this.errors.setErrors(errors);			
		}
	}
	
	public void reset() {
		setLogin("");
		setPassword("");
		errors.setErrors(null);
	}
	
	private LinkedList<String> validate() {
		LinkedList<String> errors = new LinkedList<String>();
		
		if (getLogin() == null || getLogin().isEmpty())
			errors.add("Login is empty");
		
		if (getPassword() == null || getPassword().isEmpty())
			errors.add("Password is empty");
		
		return errors;
	}
	
	public static class SubmitEvent extends GwtEvent<SubmitEvent.SubmitEventHandler> {
		public interface SubmitEventHandler extends EventHandler {
			void onSubmit(SubmitEvent event);
		}
		
		public final static Type<SubmitEventHandler> TYPE = new Type<SubmitEvent.SubmitEventHandler>();
		private final String login;
		private final String password;
		
		public SubmitEvent(String login, String password) {
			super();
			this.login = login;
			this.password = password;
		}

		@Override
		public Type getAssociatedType() {
			return TYPE;
		}

		@Override
		protected void dispatch(SubmitEventHandler handler) {		
			handler.onSubmit(this);
		}

		public String getLogin() {
			return login;
		}

		public String getPassword() {
			return password;
		}
	}
}
